﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;

using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Help。
    /// </summary>
    public class HelpDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(Help model)
        {


            using (Entities db = new Entities())
            {
                db.Helps.Add(model);
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(Help model)
        {
            using (Entities db = new Entities())
            {
                db.Helps.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Ids"></param>
        public void Delete(int[] Ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.Helps.Where(x => Ids.Contains(x.Id));

                foreach (var v in q)
                {
                    db.Helps.Remove(v);
                }

                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        /// <summary>
        /// 审核或取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool PassOrNo(int Id)
        {

            using (Entities db = new Entities())
            {
                var q = db.Helps.First(x => x.Id == Id);
                q.Pass = !q.Pass;

                db.SaveChangesAsync().Wait();
                return q.Pass;
            }
        }
        #region  加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        public Help GetInfo(int Id)
        {

            using (Entities db = new Entities())
            {
                return db.Helps.Where(x => x.Id == Id).FirstOrDefault();
            }

        }
        #endregion



        public IList<Help> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from A in db.Helps select A;


                pp.RecordCount = query.Count();


                return query.OrderByDescending(x => x.CheckInDate).Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }
    }
}
